﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Configuration; 

namespace LogScrubberCommon
{
    public class LogScrubberConfiguration : ConfigurationSection
    {
        [ConfigurationProperty("DestinationDirectory", IsRequired = true, IsKey = false)]
        public DestinationElement DestinationDirectory
        {
            get
            {
                return (DestinationElement)this["DestinationDirectory"];
            }
            set
            {
                this["DestinationDirectory"] = value;
            }
        }

        [ConfigurationProperty("SourceDirectory", IsRequired = true, IsKey = false)]
        public SourceElement SourceDirectory 
        {
            get
            { 
                return (SourceElement)this["SourceDirectory"]; 
            }
            set
            {
                this["SourceDirectory"] = value;
            }
        }
    
        [ConfigurationProperty("EnableScrubbing", IsRequired = true, IsKey = false)]
        public bool EnableScrrubbing
        {
            get
            {
                return (bool)this["EnableScrubbing"];
            }
            set
            {
                this["EnableScrubbing"] = value;
            }
        }

        [ConfigurationProperty("SaveOriginals", IsRequired = true, IsKey = false)]
        public bool SaveOriginal
        {
            get
            {
                return (bool)this["SaveOriginals"]; 
            }
            set
            {
                this["SaveOriginals"] = value; 
            }


        }

        [ConfigurationProperty("ScrubKeys", IsRequired = true, IsKey = false)]
        public LogScrubKeys ScrubKeys
        {
            get
            {
                return (LogScrubKeys)this["ScrubKeys"]; 
            }
            set
            {
                this["ScrubKeys"] = value; 
            }
        }
    }

    public class SourceElement : ConfigurationElement
    {
        [ConfigurationProperty("Path", IsRequired = true, IsKey = false)]
        public string Path
        {
            get
            {
                return (string)this["Path"];
            }
            set
            {
                this["Path"] = value; 
            }
        }

        [ConfigurationProperty("Extension", IsRequired = true, IsKey = false)]
        public string Extension
        {
            get
            {
                return (string)this["Extension"];
            }
            set
            {
                this["Extension"] = value;
            }
        }

    }

    public class DestinationElement : ConfigurationElement
    {
        [ConfigurationProperty("Path", IsRequired = true, IsKey = false)]
        public string Path
        {
            get
            {
                return (string)this["Path"];
            }
            set
            {
                this["Path"] = value;
            }
        }
    }

    public class LogScrubKeys : ConfigurationElementCollection
    {
        public LogScrubKeys()
        {



        }


        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return  ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new LogScrubKeyElement();
        }

        protected new ConfigurationElement CreateNewElement(string value)
        {
            return new LogScrubKeyElement(value);
        }

        protected Object GetElementType(LogScrubKeyElement element)
        {
            return ((LogScrubKeyElement)element).Value; 
        }

        public new string AddElementName
        {
            get
            { return base.AddElementName; }

            set
            { base.AddElementName = value; }

        }

        public new string ClearElementName
        {
            get
            { return base.ClearElementName; }

            set
            { base.AddElementName = value; }

        }

        public new string RemoveElementName
        {
            get
            { return base.RemoveElementName; }
        }

        public new int Count
        {
            get { return base.Count; }
        }

        public LogScrubKeyElement this[int index]
        {
            get
            {
                return (LogScrubKeyElement)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        new public LogScrubKeyElement this[string Name]
        {
            get
            {
                return (LogScrubKeyElement)BaseGet(Name);
            }
        }

        public int IndexOf(LogScrubKeyElement url)
        {
            return BaseIndexOf(url);
        }

        public void Add(LogScrubKeyElement url)
        {
            BaseAdd(url);
            // Add custom code here.
        }

        protected void BaseAdd(LogScrubKeyElement element)
        {
            BaseAdd(element, false);
            // Add custom code here.
        }

        public void Remove(LogScrubKeyElement url)
        {
            if (BaseIndexOf(url) >= 0)
                BaseRemove(url.Value);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void Clear()
        {
            BaseClear();
            // Add custom code here.
        }



        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((LogScrubKeyElement)element).Value; 
        }
    }

    public class LogScrubKeyElement : ConfigurationElement
    {
        public LogScrubKeyElement(string value)
        {
            Value = value;
            
        }

        public LogScrubKeyElement()
        {

        }

        [ConfigurationProperty("Value", DefaultValue = "", IsRequired = true, IsKey = false)]
        public string Value 
        {
            get { return (string)this["Value"];}
            set { this["Value"] = value;} 
        }
    


        protected override void DeserializeElement(
          System.Xml.XmlReader reader,
           bool serializeCollectionKey)
        {
            base.DeserializeElement(reader,
                serializeCollectionKey);
            // You can your custom processing code here.
        }


        protected override bool SerializeElement(
            System.Xml.XmlWriter writer,
            bool serializeCollectionKey)
        {
            bool ret = base.SerializeElement(writer,
                serializeCollectionKey);
            // You can enter your custom processing code here.
            return ret;

        }


        protected override bool IsModified()
        {
            bool ret = base.IsModified();
            // You can enter your custom processing code here.
            return ret;
        }


    }
}
